$(function () {

    //begin列表信息
    $("#bigClient_datagrid").datagrid({
        url: "/bigClient/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "schoolName", title: "学校", width: 100},
            {
                field: "important", title: "重要程度", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                }
            }
            },

            {
                field: "intention", title: "意向程度", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                }
            }
            },
            {
                field: "intentionSubject", title: "意向学科", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                }
            }
            },
            {field: "schoolTel", title: "学校电话", width: 100},
            {
                field: "linkman", title: "联系人", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                }
            }
            },
            {
                field: "marketer", title: "营销人员", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                }
            }
            },
            {
                field: "merchandiser", title: "跟进人员", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                }
            }
            },
            {field: "lastFollowTime", title: "上次跟进时间", width: 100},
            {field: "nextFollowTime", title: "下次跟进时间", width: 100},
            {field: "followStateId", title: "跟进状态", width: 100, formatter: function (value, row, index) {
                if(row.followStateId == 0){
                    return '<font color="red">未跟进</font>';
                }else{
                    return '<font color="green">已跟进</font>';
                }
            }
                },
            {field: "contractStateId", title: "客户状态", width: 100,formatter: function (value, row, index) {
                if(row.contractStateId == 0){
                    return '<font color="red">未签约</font>';
                }else{
                    return '<font color="green">已签约</font>';
                }
            }}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 10,
        pageList: [3, 5, 10, 20],
        toolbar: "#bigClient_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#bigClient_dialog").dialog({
        width: 800,
        height: 550,
        buttons: "#bigClient_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#bigClient_form").form("clear");
            $("#bigClient_dialog").dialog("setTitle", "客户添加");
            $("#bigClient_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {

            $("#bigClient_form").form("clear");
            $("#bigClient_dialog").dialog("setTitle", "客户编辑");
            var row = $("#bigClient_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if (row.important) {
                row["important.id"] = row.important.id;
            }
            if (row.intention) {
                row["intention.id"] = row.intention.id;
            }
            if (row.intentionSubject) {
                row["intentionSubject.id"] = row.intentionSubject.id;
            }
            if (row.intentionDistrict) {
                row["intentionDistrict.id"] = row.intentionDistrict.id;
            }
            if (row.linkman) {
                row["linkman.id"] = row.linkman.id;
            }
            if (row.marketer) {
                row["marketer.id"] = row.marketer.id;
            }
            if (row.merchandiser) {
                row["merchandiser.id"] = row.merchandiser.id;
            }
            if (row.education) {
                row["education.id"] = row.education.id;
            }
            if (row.schoolSystem) {
                row["schoolSystem.id"] = row.schoolSystem.id;
            }
            $("#bigClient_form").form("load", row);
            $("#bigClient_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#bigClient_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var bigClientId = $("#bigClientId").val();
            var url;
            if (bigClientId) {
                url = "/bigClient/update"
            } else {
                url = "/bigClient/save"
            }
            $("#bigClient_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#bigClient_dialog").dialog("close");
                        $("#bigClient_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove: function () {
            var row = $("#bigClient_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var bigClientId = row.id;
                    $.get("/bigClient/delete", {bigClientId: bigClientId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#bigClient_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload: function () {
            $("#bigClient_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin查看按钮
        lock: function () {
            $("#bigClient_form").form("clear");
            $("#bigClient_dialog").dialog("setTitle", "信息查看");
            var row = $("#bigClient_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要查看的行!');
                return;
            }
            $("#bigClient_form").form("load", row);
            $("#bigClient_bb").hide();
            //利用dialog 关闭按钮  解禁form表单的操作
            $('#bigClient_dialog').dialog({
                onClose: function () {
                    //启用form表单下的所用操作
                    $("input[textboxname]", $("#bigClient_form")).each(function (i, n) {
                        $(n).textbox('readonly', false);
                    });
                    //显示底部按钮
                    $("#bigClient_bb").show();
                }
            });
            $("#bigClient_dialog").dialog("open");
            //隐藏底部按钮
            $("#bigClient_bb").hide();
            //禁用form表单下的所用操作
            $("input[textboxname]", $("#bigClient_form")).each(function (i, n) {
                $(n).textbox('readonly', true);
            });
        },
        //end查看按钮

        //begin 跟进按钮
        follow: function () {
            //如果跟进状态值,为不为1  发送请求
            var row = $("#bigClient_datagrid").datagrid("getSelected");
            if (row.followStateId != 1) {
                $.get("/bigClient/updateFollowState", {id: row.id}, function (data) {
                    if (!data.success) {
                        $.messager.alert("温馨提示", data.msg);
                    } else {
                        $.messager.alert("温馨提示", '操作成功!')
                        $("#bigClient_datagrid").datagrid("reload");
                    }
                })
            }
        },
        //end跟进按钮

        //begin 签约按钮
        signed: function () {
            //如果跟进状态值,为不为1  发送请求
            var row = $("#bigClient_datagrid").datagrid("getSelected");
            if (row.contractStateId != 1) {
                $.get("/bigClient/updateContractState", {id: row.id}, function (data) {
                    if (!data.success) {
                        $.messager.alert("温馨提示", data.msg);
                    } else {
                        $.messager.alert("温馨提示", '操作成功!')
                        $("#bigClient_datagrid").datagrid("reload");
                    }
                })
            }
        },
        //end跟进按钮
        // begin 高级查询按钮
        query: function () {
            var querySchool = $("#querySchool").textbox("getValue");
            var querySubject = $("#querySubject").textbox("getValue");
            $("#bigClient_datagrid").datagrid("load", {
                querySchool: querySchool,
                querySubject: querySubject
            })
        }
        // end 高级查询
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});